package com.flutter.jl_plugin

import android.content.Context
import android.util.Log
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** JlPlugin */
class JlPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
  var TAG="JlPlugin";
  companion object {
    var channel: MethodChannel? = null
    var activity: Context? = null

  }

  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private lateinit var context: Context

  override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    if (channel == null) {
      channel = MethodChannel(flutterPluginBinding.binaryMessenger, "jl_plugin")
      channel?.setMethodCallHandler(this)
      context=flutterPluginBinding.applicationContext
      activity=context;
    }
    Log.i(TAG, "JlPlugin->onAttachedToEngine: ")

  }

  fun invokeMethod(method: String,arguments :Any){

  }
  override fun onMethodCall(call: MethodCall, result: Result) {
    Log.i(TAG,"onMethodCall:${call.method}")
    if (call.method == "initSdk") {
      JlSdkPlugin.initSdk(call,result)
    } else if(call.method=="startScanBlue"){
      //开始搜索
     JlSdkPlugin.startScanBlue(call,result)
    } else if(call.method=="stopScanBlue"){
      //停止搜索
      JlSdkPlugin.stopScanBlue(call, result)
    } else if(call.method=="connectBlue"){
      //蓝牙连接
      JlSdkPlugin.connectDevice(call,result)
    } else if(call.method=="disconnectBlue"){
      //断开蓝牙
      JlSdkPlugin.disconnectBlue(call, result)
    } else if(call.method=="configKeySettings"){
      //按键设置
      JlSdkPlugin.setKeySetting(call,result)
    } else if(call.method=="getDevicesSetting"){
      //获取设备信息
      JlSdkPlugin.getDevicesSetting(call, result)
    } else if(call.method=="sendCustomCmd"){
      //发送自定义指令
      JlSdkPlugin.sendCustomCmd(call,result)
    } else if(call.method=="eqSetting"){
      //EQ设置
      JlSdkPlugin.eqSetting(call,result)
    } else if(call.method=="getEQSetting"){
      //获取EQ设置
      JlSdkPlugin.getEQSetting(call, result)
    } else if(call.method=="modifyHeadsetFunctions"){
      //功能设置
      JlSdkPlugin.modifyHeadsetFunctions(call, result)
    } else if(call.method=="changeDeviceName"){
      //修改设备名字
      JlSdkPlugin.changeDeviceName(call, result)
    } else if(call.method=="rebootDevice"){
      //重启设备
      JlSdkPlugin.rebootDevice(call, result)
    } else if(call.method=="getCurrentDevices"){
      //获取当前设备列表
      JlSdkPlugin.getCurrentDevices(call, result)
    } else if(call.method=="getHistoryBluetoothDeviceList"){
      //获取历史设备列表
      JlSdkPlugin.getHistoryBluetoothDeviceList(call, result)
    } else if(call.method=="removeHistoryBluetoothDevice"){
      //移除历史设备
      JlSdkPlugin.removeHistoryBluetoothDevice(call, result)
    } else if(call.method=="findHistoryBluetoothDevice"){
      //获取历史设备
      JlSdkPlugin.findHistoryBluetoothDevice(call, result)
    } else if(call.method=="isConnectioned"){
      //是否连接
      JlSdkPlugin.isConnectioned(call,result)
    } else if(call.method=="isConnecting"){
      //是否正在连接
      JlSdkPlugin.isConnecting(call,result)
    } else if(call.method=="isScanning"){
      //是否正在扫描
      JlSdkPlugin.isScanning(call,result)
    } else if(call.method=="isInit"){
      //是否初始化成功
      JlSdkPlugin.isInit(call,result)
    } else if(call.method=="stopReconnect"){
      //停止重连
      JlSdkPlugin.stopReconnect(call,result)
    }else if(call.method=="startReconnect"){
      //打开重连机制
      JlSdkPlugin.startReconnect(call,result)
    }else if(call.method=="startRecord"){
      //开始录音
      JlSdkPlugin.startRecord(call,result)
    }else if(call.method=="stopRecord"){
      //停止录音
      JlSdkPlugin.stopRecord(call,result)
    } else if(call.method=="startDecode"){
      //开始opus解码
      OpusTools.initOpus()
      OpusTools.startDecode()
      result.success(true)
    } else if(call.method=="stopDecode"){
      //停止解码
      OpusTools.stopDecode()
      result.success(true)
    }else if(call.method=="releaseDecode"){
      //销毁
      OpusTools.release()
      result.success(true)
    }else if(call.method=="decodeOpus"){
      val pcmData: ByteArray? = call.argument("data")
      OpusTools.decodeOpus(pcmData)
      result.success(true)
    }
  }

  override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
    Log.i(TAG, "onDetachedFromEngine: ")
//    Instance.channel.setMethodCallHandler(null)
  }

  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    Log.i(TAG, "onAttachedToActivity: ")
    context=binding.activity
    JlSdkPlugin.context=context;
  }

  override fun onDetachedFromActivityForConfigChanges() {
    Log.i(TAG, "onDetachedFromActivityForConfigChanges: ")
  }

  override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    Log.i(TAG, "onReattachedToActivityForConfigChanges: ")
  }

  override fun onDetachedFromActivity() {
    Log.i(TAG, "onDetachedFromActivity: ")
  }

}
